//------------------------------------------------------------------------------
// <copyright company="Telligent Systems">
//     Copyright (c) Telligent Systems Corporation.  All rights reserved.
// </copyright> 
//------------------------------------------------------------------------------

using CommunityServer.Blogs.Components;
using CommunityServer.Components;

namespace CommunityServer.Blogs.Controls
{
    /// <summary>
    /// Summary description for ViewBlogPost.
    /// </summary>
    public class EntryViewContainer :  WeblogThemedControl
    {
        private EntryComments comments = null;
        private EntryView entry = null;
        private BlogPostToolBar ToolBar = null;
        private AboutAuthor aboutAuthor = null;
        private CommentForm form = null; 
        private DetailedPostSubscription DetailedSubBar = null;

        public EntryViewContainer():base()
        {
			
        }

        protected override void AttachChildControls()
        {
            aboutAuthor = FindControl("Author") as AboutAuthor;
            comments = FindControl( "Comments" ) as EntryComments;
            entry = FindControl( "Entry" ) as EntryView;
            form = FindControl( "form" ) as CommentForm;
            ToolBar = FindControl("ToolBar") as BlogPostToolBar;
            DetailedSubBar = FindControl("DetailedSubBar") as DetailedPostSubscription;
            
            if(comments == null || entry == null)
                return;

            BindData();
        }

        void BindData()
        {
        	CSContext cntx = CSContext.Current;

            BlogThreadQuery query = new BlogThreadQuery();
            query.PostID = cntx.PostID;

            if(!Globals.IsNullorEmpty(this.CurrentCSContext.PostName))
                query.PostName = this.CurrentCSContext.PostName;

            query.IncludeCategories = true;
            query.ReturnFullThread = true;
            query.SectionID = CurrentWeblog.SectionID;

            
            PostSet ps =  WeblogPosts.GetPosts(query,true);
            if(ps != null)
            {
                ps.Organize();

                WeblogPost we = ps.ThreadStarter as WeblogPost;

                entry.DataSource = we;
                entry.DataBind();

                if(we != null && form != null)
                {
                    form.DataSource = we;
                    form.DataBind();
                }

                if(aboutAuthor != null && we != null)
                {
                    aboutAuthor.UserName = we.Username;
                    aboutAuthor.DataBind();
                }
                if(CurrentWeblog.EnableCommentsDefault)
                {
                    comments.DataSource = ps;
                    comments.DataBind();
                }

                if(ToolBar != null)
                {
                    ToolBar.CurrentPost = we;
                    ToolBar.DataBind();
                }

                if(DetailedSubBar != null)
                {
                    DetailedSubBar.CurrentPost = we;
                    DetailedSubBar.DataBind();
                }

                this.SetPageTitle(we.Subject);

				// Add PingBack URL to HTTP Header and body
				string baseUrl = Globals.HostPath(Context.Request.Url);
				Context.Response.AddHeader("X-Pingback", baseUrl + BlogUrls.Instance().PingBackServiceUrl(CurrentWeblog.ApplicationKey));
				CommunityServer.Controls.Head.AddLinkTag("pingback", baseUrl + BlogUrls.Instance().PingBackServiceUrl(CurrentWeblog.ApplicationKey), Context);

                Referral r = new Referral();
                r.SettingsID = cntx.SettingsID;
                r.SectionID = we.SectionID;
                r.PostID = we.PostID;
                Referrals.AddToQueue(Context,CurrentWeblog.ReferralFilter,r);
                
            }
        }

    }
}
